const config_rules = require('./config.rules')
const config_plugins = require('./config.plugins')
const config_resolve = require('./config.resolve')
const config_externals = require('./config.externals')
const config_optimization = require('./config.optimization')
const { resolve, isLocal } = require('./utils')

const config = {
  target: 'web',
  mode: isLocal ? 'development' : 'production',
  entry: {
    app: resolve('src/app/app.tsx')
  },
  output: {
    path: resolve('dist'),
    filename: isLocal ? '[name]-[hash:6].js' : 'js/[name]-[chunkhash:6].js',
    publicPath: isLocal ? '/' : './'
  }
}

if (isLocal) {
  config.devtool = 'cheap-module-eval-source-map'
  config.devServer = {
    clientLogLevel: 'info',
    compress: true,
    overlay: {
      warnings: true,
      errors: true
    },
    stats: 'errors-only',
    port: 7000,
    host: '0.0.0.0',
    contentBase: resolve('dist'),
    historyApiFallback: {
      index: '/'
    },
    hot: true,
  }
  config.watch = true
}

config.resolve = config_resolve
config.module = { rules: config_rules }
config.plugins = config_plugins
config.optimization = config_optimization
config.externals = config_externals

module.exports = config
